﻿package com.unifiedfield{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.events.*;
	public class SoundPlayer {
		private var thesound:Sound;
		private var thechannel:SoundChannel;
		private var playlist:Array;
		private var at;
		private var playonload;
		public function SoundPlayer(){
			playlist=new Array();
			at=0;
			playonload=false;
		}
		public function addSound(path){
			playlist.push(path);
		}
		public function loadSound(a,p){
			playonload=p;
			if(a>-1&&a<=at){
				thesound=new Sound(new URLRequest(playlist[at]));
				thesound.addEventListener(Event.COMPLETE,completeHandler);
			}else{
				return false;
			}
		}
		public function stopSound(){
			if(thechannel!=null){
				thechannel.stop();
			}
		}
		public function clearPlayer(){
			thesound=null;
			thechannel=null;
			playlist=new Array();
			at=0;
			playonload=false;
		}
		private function completeHandler(e){
			if(playonload){
				thechannel=thesound.play();
				thechannel.addEventListener(Event.SOUND_COMPLETE,soundcompeteHandler);
			}
		}
		private function soundcompeteHandler(e){
			at++;
			if(at<playlist.length){
				loadSound(at,true);
			}
		}
	}
}